package com.SouthernWall_404.Age_Capitalism.common.Network;

import com.SouthernWall_404.Age_Capitalism.AgeCapitalism;
import com.SouthernWall_404.Age_Capitalism.common.Network.C2S.AddingMerchantPacket;
import com.SouthernWall_404.Age_Capitalism.common.Network.C2S.MerchantClientPacket;
import com.SouthernWall_404.Age_Capitalism.common.Network.S2C.MerchantServerPacket;
import com.SouthernWall_404.Age_Capitalism.common.Network.S2C.StoreBlockEntityPacket;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.network.NetworkRegistry;
import net.minecraftforge.network.PacketDistributor;
import net.minecraftforge.network.simple.SimpleChannel;

public class ModChannels {

    private static final String PROTOCOL_VERSION = "1";
    private static int id=0;


    public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(
            new ResourceLocation(AgeCapitalism.MODID, "main"),
            () -> PROTOCOL_VERSION,
            PROTOCOL_VERSION::equals,
            PROTOCOL_VERSION::equals
    );

    public static void register()
    {
        INSTANCE.registerMessage(id++, MerchantServerPacket.class,
                MerchantServerPacket::encode,
                MerchantServerPacket::new,
                MerchantServerPacket::handle
        );
        INSTANCE.registerMessage(id++, MerchantClientPacket.class,
                MerchantClientPacket::encode,
                MerchantClientPacket::new,
                MerchantClientPacket::handle
        );

        INSTANCE.registerMessage(id++, StoreBlockEntityPacket.class,
                StoreBlockEntityPacket::encode,
                StoreBlockEntityPacket::new,
                StoreBlockEntityPacket::handle
        );

        INSTANCE.registerMessage(id++, AddingMerchantPacket.class,
                AddingMerchantPacket::encode,
                AddingMerchantPacket::new,
                AddingMerchantPacket::handle
        );


    }


    public static <MSG> void sendToServer(MSG message)
    {
        INSTANCE.sendToServer(message);
    }
    public static <MSG> void sendToClient(MSG message, Player player)
    {
        INSTANCE.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), message);}

}
